﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using TrangTrai.Data.Utils;
using TrangTrai.Data.Models;
namespace TrangTrai.Data.DAO
{
    public class GroupManagementDAO
    {
	
        
        SqlConnection con;
        SqlCommand command;
        SqlDataReader re;
         public static OrgManagementDataContext context = new OrgManagementDataContext(ConnectionClass.getConnectionString());
        public GroupManagementDAO() {
            //init();
        }
        private void init(){
            con = ConnectionClass.getConnection();
            command = con.CreateCommand();
        }
        #region groupManagement
       

        public GroupInfo[] getAllGroupListByLinq()
        {
            var groupResult = context.getGroupInfos().ToArray();
            return groupResult;
        }


        public void createGroup(GroupInfo groupInfo)
        {
            context.createGroup(groupInfo.groupCode, groupInfo.groupName, groupInfo.address, groupInfo.note);
        }

        public GroupInfo[] GetGroupByCode(string groupCode)
        {
            return context.GetGroupByCode(groupCode).ToArray();
        }
        public void deleteGroup(int groupId)
        {
            context.DeleteGroup(groupId);
        }
        public GroupInfo getGroupById(int groupId)
        {
            return context.GetGroupById(groupId).ToArray().First();
        }
        public void deleteUsersFromGroup(int groupId) {
            context.RemoveUserFromGroup(groupId);
        }

        public void updateGroup(GroupInfo groupInfo) {
            context.updateGroup(groupInfo.groupId, groupInfo.groupCode, groupInfo.groupName, groupInfo.address, groupInfo.note);
        }

        #endregion
        #region UserManagement
        public UserInfo[] getUsersInGroup(int groupId)
        {
            return context.GetUsersInGroup(groupId).ToArray();
        }
        public UserInfo[] getUserByNameResult(String userName)
        {
            return context.getUserByName(userName).ToArray();
        }
        public void addUsersIntoGroup(int groupId, List<UserInfo> currentUserList)
        {
            if (groupId != 0)
            {
                for (int i = 0; i < currentUserList.Count; i++)
                {
                    context.addUserIntoGroup(currentUserList.ElementAt(i).userId, groupId);
                }
            }

        }
        public UserInfo[] getAllUsers()
        {
            return context.getAllUsers().ToArray();
        }

        public void addUser(UserInfo userInfo) {
            context.InsertUser(userInfo.fullName, userInfo.userCode, userInfo.password, userInfo.phone, userInfo.address, userInfo.roleId, userInfo.roleId);
                
        }
        public void updateUser(UserInfo userInfo) {
            
            context.updateUser(userInfo.userId, userInfo.fullName, userInfo.userCode, userInfo.password, userInfo.phone, userInfo.address, userInfo.userStatusId, userInfo.roleId);
        }

        public void deleteUser(int userId)  
        {
            context.deleteUser(userId);
        }

        #endregion
        #region role,User status
        //get roles
        public Role[] getRoles()
        {
            return context.getRoles().ToArray();
        }
        public UserStatus[] getUserStatus()
        {
            return context.getUserStatus().ToArray();
        } 
        #endregion
       
        #region Aomanagement
        public void createAo(AoInfo aoInfo)
        {
            context.createAO(aoInfo.aoCode, aoInfo.aoName, aoInfo.createdDate, aoInfo.aoStatusId, aoInfo.note, aoInfo.dienTich, aoInfo.doSau, aoInfo.groupId, aoInfo.seasonId == null ? -1 : aoInfo.seasonId);
        }
        public AoStatus[] getAoStatus()
        {
            return context.getAoStatus().ToArray();
        }
        public AoManagement[] getAoManagementList() {
            return context.getAoList().ToArray();
        }
        public SeasonInfo[] getSeasonList() {
            return context.getSeasonInfos().ToArray();
        }
        public void updateAoSeason (AoManagement aoMan){
            
            AoSeasonInfo[] temp= context.getAoSeasonById(aoMan.aoSeasonId).ToArray();
            if(temp.Length>0){
                    context.updataAoSeason(temp[0].aoId, aoMan.seasonId, aoMan.groupId, aoMan.aoStatusId, aoMan.aoSeasonId,aoMan.note);
            }
            
        }
        public AoEntry[] getAoList() { 
            //context.get
            return context.getAoEntryList().ToArray();
        }

        public AoInfo []getAoById(int aoId) {
            return context.getAoById(aoId).ToArray();
        }
        public TomInfo []getTomList() {
            return context.getTomList().ToArray();
        }
        #region kich hoat ao
        public void activeAo(ThaTom thatomInfo)
        {
            //int result = context.kichHoatThatom(thatomInfo.aoId, thatomInfo.seasonId, thatomInfo.tomId, thatomInfo.ngayTha, thatomInfo.groupId, thatomInfo.amount, thatomInfo.note, thatomInfo.mucnuoc);
        }
        public AoInfo[] getActiveAoList()
        {
            return context.getActiveAoList().ToArray();
        } 
        #endregion
        #region tia ao
        public void TiaAo(TiaAo tiaAo, int oldAoId, int oldSeasonId)
        {

            context.DoTiaAo(tiaAo.aoId, tiaAo.seasonId, (int)tiaAo.remain, tiaAo.aoName, tiaAo.ngaytia, tiaAo.note, oldAoId, oldSeasonId);
        }
        public ThaTom getThaTomById(int aoId, int seasonId)
        {
            ThaTom[] arr = context.getThatom(aoId, seasonId).ToArray();
            return arr.Length == 1 ? context.getThatom(aoId, seasonId).ToArray().First() : null;

        #endregion
        }
        #endregion
    }
}
